package net.easyjava.framework.third.aliyun;

import com.aliyun.oss.model.PartETag;
import net.easyjava.framework.utils.ProjectProperties;
import net.easyjava.third.aliyun.oss.AliyunOSSClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxnacy on 16/7/18.
 */
public class AliyunOSSUtils {
    private static Logger logger = LoggerFactory.getLogger(AliyunOSSUtils.class);
    static AliyunOSSClient client;
    static String endpoint;
    static {
        if(client == null){
            init();
        }
    }
    private static synchronized void init(){
        if(client == null){
            ProjectProperties properties = ProjectProperties.getInstance();
            String key = properties.getProperty("aliyun.key.id");
            String secret = properties.getProperty("aliyun.key.secret");
            endpoint = properties.getProperty("aliyun.endpoint");
            try {
                client = AliyunOSSClient.getInstance(key,secret);
            } catch (Exception e) {
                logger.error("AliyunOSSClient 初始化失败 key:{} secret:{}",key,secret);
                logger.error(e.getMessage(),e.fillInStackTrace());
            }
        }
    }

    /**
     * 通过地址上传文件
     * @param bucket
     * @param pathOrUrl
     * @return
     */
    public static String upload(String bucket,String pathOrUrl){
        return upload(bucket, pathOrUrl,null);
    }
    public static String upload(String bucket,String pathOrUrl,String fileName){
        String key = "";
        try {
            if(pathOrUrl.startsWith("http://")){
               key =  client.upload("http://"+endpoint, bucket, new URL(pathOrUrl),fileName);
            }else{
                key = client.upload("http://"+endpoint, bucket, new File(pathOrUrl),fileName);
            }

        } catch (IOException e) {
            logger.error("AliyunOSSClient 上传文件失败 bucket:{} path:{}",bucket,pathOrUrl);
            logger.error(e.getMessage(),e.fillInStackTrace());e.printStackTrace();
        }
        return key;
    }

    /**
     * 通过网页上传
     * @param bucket
     * @param request
     * @param inputName input域的名字
     * @return
     */
    public static String upload(String bucket,HttpServletRequest request,String inputName){
        String key = "";
        try {
            key = client.uploadByRequest("http://"+endpoint,bucket,request,inputName,null);

        } catch (Exception e) {
            logger.error("AliyunOSSClient 上传文件失败 inputName:{}",inputName);
            logger.error(e.getMessage(),e.fillInStackTrace());e.printStackTrace();
        }
        return key;
    }

    /**
     * 通过流上传文件
     * @param endpoint
     * @param bucket
     * @param inputStream
     * @param fileName    文件名
     * @return
     */
    public static String upload(String endpoint, String bucket, InputStream inputStream, String fileName) {
        String key = "";
        try {
            key = client.upload(endpoint, bucket, inputStream, fileName);
        } catch (Exception e) {
            logger.error("AliyunOSSClient 通过流上传文件失败 ");
        }
        return key;
    }


    /**
     * 获取分片上传uploadId
     * @param bucket
     * @param key
     * @return
     */
    public static String getMultiUploadId(String bucket,String key){
        String uploadId = "";
        try {
            uploadId = client.getMultipartUploadId("http://"+endpoint, bucket, key);

        } catch (Exception e) {
            logger.error("AliyunOSSClient 获取分片上传uploadId失败 key:{},e:{}",key,e.fillInStackTrace());
            logger.error(e.getMessage(),e.fillInStackTrace());
        }
        return uploadId;
    }


    /**
     * 上传分片
     * @param bucket
     * @param uploadId
     * @param is InputStream流
     * @param size 大小
     * @param partNumber 分片位置
     * @param key aliyunoss key
     * @return
     */
    public static PartETag uploadMultiPart(String bucket,String uploadId,InputStream is,long size,int partNumber,String key){
        try {
            PartETag partETag = client.uploadMultiPart("http://"+endpoint,bucket,uploadId,is,size,partNumber,key);
            return partETag;
        } catch (Exception e) {
            logger.error("AliyunOSSClient 上传分片失败 uploadId:{},size:{},partNumber:{},key:{}"
                    ,uploadId,size,partNumber,key,e.fillInStackTrace());
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }
    }

    /**
     * 获取uploadId下的所有上传的分片
     * @param bucket
     * @param uploadId
     * @param key
     * @return
     */
    public static List<PartETag> getMultiParts(String bucket,String uploadId,String key){
        try {
            List<PartETag> partETags = client.getMultiParts("http://"+endpoint, bucket, uploadId, key);
            return partETags;
        } catch (Exception e) {
            logger.error("AliyunOSSClient 获取uploadId下的所有上传的分片 uploadId:{},key:{}"
                    ,uploadId,key,e.fillInStackTrace());
            logger.error(e.getMessage(),e.fillInStackTrace());
            return new ArrayList<>();
        }
    }


    /**
     * 完成分片的最后上传
     * @param bucket
     * @param uploadId
     * @param partETags
     * @param key
     * @return
     */
    public static String uploadMulti(String bucket,String uploadId,List<PartETag> partETags,String key){
        try {
            client.uploadMulti("http://"+endpoint, bucket, uploadId, partETags, key);
            return key;
        } catch (Exception e) {
            logger.error("AliyunOSSClient 完成分片上传失败 uploadId:{},partETags:{},key:{},e:{}",uploadId,partETags,key,e.fillInStackTrace());
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }
    }











}
